package com.gopay.bis.manager.bank.payconfig.impl;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.gopay.bis.dao.payconfig.BisExpaySmsSendLogDAO;
import com.gopay.bis.manager.bank.payconfig.ValidCodeSupportService;
import com.gopay.common.constants.bank.SmsSendSourceConstants;
import com.gopay.common.constants.cust.SMSType;
import com.gopay.common.constants.validcode.CodeType;
import com.gopay.common.domain.bank.ExPayBaseMessage;
import com.gopay.common.domain.log.BisExpaySmsSendLog;
import com.gopay.common.domain.vs.VsValidCodeDtl;
import com.gopay.common.security.validcode.service.ValidCodeService;

/**
 * 
 * @author w_jiqing
 *
 */
@Service("validCodeSupportService")
public class ValidCodeSupportServiceImpl implements ValidCodeSupportService {
  
    @Resource(name = "validCodeService")
    private ValidCodeService validCodeService;
    
    @Resource(name = "bisExpaySmsSendLogDAO")
    private BisExpaySmsSendLogDAO bisExpaySmsSendLogDAO;

    private static Logger log = Logger.getLogger(ValidCodeSupportServiceImpl.class);


    
    public String createValidCode(ExPayBaseMessage baseMsg) {
        boolean res = false;
        VsValidCodeDtl codeDtl = null;
        String code = null;
        String mobileNo = baseMsg.getMediaId();
        if (StringUtils.isBlank(mobileNo)) {
            log.info("send sms mobileNo 为空;result:" + res);
            return null;
        }
        if (!checkMobile(mobileNo)) {
            log.info("send sms mobileNo:" + mobileNo + ";result:" + res);
            return null;
        }
        codeDtl = validCodeService.generate(CodeType.PAY, baseMsg.getOrderId());
        if (StringUtils.isNotBlank(codeDtl.getCode())) {
            code = codeDtl.getCode();
            res = true;
        }
        log.info("send sms mobileNo:" + mobileNo + ";result:" + res);

        return code;
    }

    
    public boolean checkSMS(ExPayBaseMessage baseMsg) {
        boolean res = false;
        String mobileNo = baseMsg.getMediaId();
        if (StringUtils.isBlank(mobileNo)) {
            log.info("send sms mobileNo 为空;result:" + res);
            return res;
        }
        if (!checkMobile(mobileNo)) {
            log.info("check sms mobileNo:" + mobileNo + ";result:" + res);
            return res;
        }
        res = validCodeService.valid(CodeType.PAY, baseMsg.getOrderId(), baseMsg.getValidateCode(), baseMsg.getTimeOut());

        if(!res){
            buildCheckLog(baseMsg,SMSType.CASHIER_SMS_ERROR);
        }
        return res;
    }



    public static boolean checkMobile(String mobile) {
        if (StringUtils.isBlank(mobile)) {
            return false;
        }
        String regex = "^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\\d{8}$";

        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(mobile);

        return m.matches();
    }
    
    
    /** 
     * 验证码检验不通过，插入一条校验不通过的日志
     * @param baseMsg
     * @param smsType 
     */
    private void buildCheckLog(ExPayBaseMessage baseMsg,SMSType smsType){
        BisExpaySmsSendLog log = new BisExpaySmsSendLog();
        Date date = new Date();
        log.setMobileNum(baseMsg.getMediaId());
        log.setModuleId(smsType.value);
        log.setOrderId(baseMsg.getOrderId());
        log.setCardNo(baseMsg.getCardNo());
        log.setOperateSource(SmsSendSourceConstants.CHECK_SOURCE_GFB);
        log.setCreatedTime(date);
        bisExpaySmsSendLogDAO.save(log);
    }


}
